package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.UsercollectionMapper;
import com.qf.pojo.Usercollection;
import com.qf.service.UsercollectionService;
import com.qf.utils.RandomUtil;
import com.qf.utils.enums.CheckStatus;
import com.qf.utils.enums.FileStatus;
import com.qf.utils.vo.PersonnelDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/21 0021
 * Author->DT
 * qmxc
 * 行到水穷处 坐看云起时
 * 模块：人事档案Service层实现
 */
@Service
public class UsercollectionServiceImpl implements UsercollectionService {
    @Autowired
    private UsercollectionMapper mapper;
    //添加档案信息设置状态为起草
    @Override
    public int insert(Usercollection record) {
        //设置审核状态为起草
        record.setCheckstatus(CheckStatus.DRAFT.getStatus());
        //档案编码自动生成
        record.setDanganid(RandomUtil.random());
        record.setAddtime(new Date());
        //新增
        int count = mapper.insert(record);
        return count;
    }

    //添加并将审核状态设置为审核中
    @Override
    public int insertAndSetCheckState(Usercollection usercollection) {
        //设置审核状态为审核中
        usercollection.setCheckstatus(CheckStatus.CHECKING.getStatus());
        //档案编码自动生成
        usercollection.setDanganid(RandomUtil.random());
        usercollection.setAddtime(new Date());
        //新增
        int count = mapper.insert(usercollection);
        return count;
    }

    //将审核状态设置为审核中
    @Override
    public int setCheckState(Usercollection usercollection) {
        //设置审核状态为审核中
        usercollection.setCheckstatus(CheckStatus.CHECKING.getStatus());
        //修改信息
        int count = mapper.updateByPrimaryKeySelective(usercollection);
        return count;
    }

    //将档案状态设置为已删除
    @Override
    public int updateCheckStateToDelete(Integer id) {
        Usercollection usercollection = new Usercollection();
        //设置档案id
        usercollection.setId(id);
        //设置档案状态为已删除
        usercollection.setStatus(FileStatus.DELETE_STAFF.getStatus());
        //将审核状态设置为审核中：人事审核模块只有审核中的档案能显示
        usercollection.setCheckstatus(CheckStatus.CHECKING.getStatus());
        //修改信息
        int count = mapper.updateByPrimaryKeySelective(usercollection);
        return count;
    }

    //删除恢复:根据id修改档案状态为试用期;
    @Override
    public int recoverDeleteState(Integer id) {
        Usercollection usercollection = new Usercollection();
        //设置档案id
        usercollection.setId(id);
        //设置档案状态为试用期(可以在删除时将原来的档案状态保存在一张表中，恢复的时候根据id查出删除前档案 的状态)
        usercollection.setStatus(FileStatus.TRY_PERIOD.getStatus());
        //将审核状态恢复为起草
        usercollection.setCheckstatus(CheckStatus.DRAFT.getStatus());
        //修改信息
        int count = mapper.updateByPrimaryKeySelective(usercollection);
        return count;
    }

    //逻辑删除的档案进行永久删除
    @Override
    public int foreverDelete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    //档案变更(修改信息并将审核状态设置为变更)
    @Override
    public int modify(Usercollection usercollection) {
        //设置审核状态为变更
        usercollection.setCheckstatus(CheckStatus.MODIFY.getStatus());
        //修改信息
        int count = mapper.updateByPrimaryKeySelective(usercollection);
        return count;
    }

    //进行变更并提交申请(修改信息+修改审核状态为变更审核中)
    @Override
    public int modifyAndChecking(Usercollection usercollection) {
        //设置审核状态为变更审核中
        usercollection.setCheckstatus(CheckStatus.MODIFY_CHECKING.getStatus());
        //修改信息
        int count = mapper.updateByPrimaryKeySelective(usercollection);
        return count;
    }

    //人事管理模块查询人事档案
    @Override
    public PageInfo<PersonnelDataVO> findPersonnelInfoByLimit(Map map) {
        //开启分页
        PageHelper.startPage((int)map.get("index"), (int)map.get("page"));
        //查询人事信息
        List<PersonnelDataVO> list=mapper.findPersonnelInfoByLimit(map);
        //封装PageInfo
        PageInfo<PersonnelDataVO> pageInfo = new PageInfo<>(list);
        //返回
        return pageInfo;
    }

    //人事审核模块查询指定状态(审核中)的人事信息
    @Override
    public PageInfo<PersonnelDataVO> findPersonnelInfoByLimitForCheck(Map map) {
        //开启分页
        PageHelper.startPage((int)map.get("index"), (int)map.get("page"));
        //查询人事信息
        List<PersonnelDataVO> list=mapper.findPersonnelInfoByLimitForCheck(map);
        //封装PageInfo
        PageInfo<PersonnelDataVO> pageInfo = new PageInfo<>(list);
        //返回
        return pageInfo;

    }

    //根据档案id查询档案信息
    @Override
    public Usercollection selectByPrimaryKey(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }



    @Override
    @Transactional
    public int updateByPrimaryKeySelective(Usercollection record) {
        return mapper.updateByPrimaryKeySelective(record);
    }


    //审核档案通过
    @Override
    @Transactional
    public int updateCheckStateToPass(Integer id) {
        Usercollection usercollection = new Usercollection();
        //封装参数
        usercollection.setId(id);
        usercollection.setCheckstatus(CheckStatus.CHECKING_PASS.getStatus());
        //执行修改
        int count = mapper.updateByPrimaryKeySelective(usercollection);
        return count;
    }

    @Override
    @Transactional
    public int updateCheckStateToBack(Integer id) {
        Usercollection usercollection = new Usercollection();
        //封装参数
        usercollection.setId(id);
        usercollection.setCheckstatus(CheckStatus.CHECKING_REJECT.getStatus());
        //执行修改
        int count = mapper.updateByPrimaryKeySelective(usercollection);
        return count;
    }


}
